import { format, createLogger, transports } from 'winston';

import DailyRotateFile from 'winston-daily-rotate-file'
import path from 'path'
const LOGS_FOLDER = path.resolve('logs');

const fileFormat = format.combine(
    format.timestamp(),
    format.json()
);

const consoleFormat = format.combine(
    format.colorize({ all: true }),
    format.timestamp({ format: 'HH:mm:ss' }),
    format.printf(({ timestamp, level, message, ...metadata }) => {
        return `[${timestamp}] ${level}: ${typeof message === 'object'
            ? JSON.stringify(message)
            : message} ${Object.keys(metadata).length ? JSON.stringify(metadata) : ''}`;
    })
);

const logger = createLogger({
    level: 'info',
    transports: [
        new transports.Console({
            format: consoleFormat
        }),
        new DailyRotateFile({
            dirname: LOGS_FOLDER,
            filename: '%DATE%.log',
            datePattern: 'YYYY-MM-DD',
            zippedArchive: false,
            maxSize: '20m',
            maxFiles: '14d',
            format: fileFormat
        })
    ]
});;

export default logger
